package com.nondo.dean.saprfc.common;


import com.nondo.dean.saprfc.domain.ScheduleBean;
import com.nondo.dean.saprfc.mapper.ScheduleProvider;
import com.nondo.dean.saprfc.service.TestQuartz;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * @author :xiaofan
 */
@Component
public class ScheduleManage {
	private static ScheduleManage scheduleManage = null;
	private Scheduler scheduler =null;
	private ScheduleManage() {
	}

	public static ScheduleManage getScheduleManage() {
		if (scheduleManage == null) {
			scheduleManage = new ScheduleManage();
		}
		return scheduleManage;
	}

	private Class getClass(String str) {
		Class<?> class1 = null;
		try {
			class1 = Class.forName(str);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return class1;
	}

	public void start() {
		try {
			ArrayList<ScheduleBean> arraylist = ScheduleProvider.getSchedule();
			scheduler = StdSchedulerFactory.getDefaultScheduler();
			for (ScheduleBean bean : arraylist) {
				String v_classId = bean.getClassId();
				String v_classPath = bean.getClassPath();//执行任务类路径
				String v_cronexpr = bean.getCronExpr();//cron表达式
				JobDetail jobdetail = JobBuilder.newJob(getClass(v_classPath))
						.withIdentity("JobName_" + v_classId, "group1").build();
				CronScheduleBuilder cronBuilder = CronScheduleBuilder
						.cronSchedule(v_cronexpr);
				Trigger cronTrigger = TriggerBuilder.newTrigger()
						.withIdentity("TriggerName_" + v_classId, "group1")
						.withSchedule(cronBuilder).build();
				scheduler.scheduleJob(jobdetail, cronTrigger);
			}
			if (arraylist.size() > 0) {
				scheduler.start();

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void go(){

		try {
			SchedulerFactory sf = new StdSchedulerFactory();
			Scheduler sched = sf.getScheduler();
			//jobs可以在scheduled的sched.start()方法前被调用

			//job 1将每隔20秒执行一次
			JobDetail job = newJob(TestQuartz.class).withIdentity("job1", "group1").build();
			CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(cronSchedule("0/20 * * * * ?")).build();
			Date ft = sched.scheduleJob(job, trigger);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
			System.out.println(job.getKey() + " 已被安排执行于: " + sdf.format(ft) + "，并且以如下重复规则重复执行: " + trigger.getCronExpression());

			// job 2将每2分钟执行一次（在该分钟的第15秒)
			job = newJob(TestQuartz.class).withIdentity("job2", "group1").build();
			trigger = newTrigger().withIdentity("trigger2", "group1").withSchedule(cronSchedule("15 0/2 * * * ?")).build();
			ft = sched.scheduleJob(job, trigger);
			System.out.println(job.getKey() + " 已被安排执行于: " + sdf.format(ft) + "，并且以如下重复规则重复执行: "+ trigger.getCronExpression());


			sched.start();
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}
	public void stop() {
		try {
			if(scheduler!=null){
				scheduler.shutdown();

			}
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args){
		new ScheduleManage().go();
	}

}
